﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ActivitiesControl.GUI.Silverlight.RemoteServices;
using ActivitiesControl.GUI.Silverlight.Utils;
using ActivitiesControl.GUI.Silverlight.Views;
using System.Windows.Browser;

namespace ActivitiesControl.GUI.Silverlight
{
	public partial class Main 
		: UserControl
	{
		private static volatile Main instance;
		private static object syncRoot = new Object();

		public static Main Instance
		{
			get
			{
				if (instance == null)
				{
					lock (syncRoot)
					{
						if (instance == null)
							instance = new Main();
					}
				}

				return instance;
			}
		}

		private Main()
		{
			InitializeComponent();
			ReportHyperlinkButton.NavigateUri = new Uri(HtmlPage.Document.DocumentUri.ToString() + "Reports");
			SetContent(new Login());
		}

		public void SetContent(Page page)
		{
			ContentGrid.Children.Clear();
			ContentGrid.Children.Add(page);

			TitleLabel.Content = page.Title;

			ReportHyperlinkButton.Visibility = Visibility.Collapsed;

			if (LoggedUser.Instance.User != null)
			{
				ChangePasswordtHyperlinkButton.Visibility = LogoutHyperlinkButton.Visibility = Visibility.Visible;
				UsernameLabel.Visibility = Visibility.Visible;
				UsernameLabel.Content = string.Format("Bem-vindo(a), {0}", LoggedUser.Instance.User.Resource.Name);

				if (LoggedUser.Instance.User.UserType == UserType.Manager)
					ReportHyperlinkButton.Visibility = Visibility.Visible;
			}
			else
			{
				ChangePasswordtHyperlinkButton.Visibility = LogoutHyperlinkButton.Visibility = Visibility.Collapsed;
				UsernameLabel.Visibility = Visibility.Collapsed;
				UsernameLabel.Content = string.Empty;
			}
		}

		private void LogoutHyperlinkButton_Click(object sender, RoutedEventArgs e)
		{
			LoggedUser.Instance.User = null;
			SetContent(new Login());
			ShowMessage("Você efeutou o logout do sistema.");
		}

		public void ShowMessage(string message)
		{
			MessageText.Text = string.Format("@{0}: {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), message);
		}

		private void ChangePasswordtHyperlinkButton_Click(object sender, RoutedEventArgs e)
		{
			ChangePassword changePassword = new ChangePassword();
			changePassword.Show();
		}
	}
}